{extend name="main"}
{block name="style"}
<style type="text/css">
	.w-80px{width: 80px;}
</style>
{/block}
{block name="body"}
<div class="bg-white p-3">
<el-form :model="form" @submit.native.prevent="submitForm" :rules="rules" ref="form" label-width="120px" :label-position="labelPosition">

  <el-form-item label="规则名称" prop="title">
    <el-input v-model="form.title" class="col-12 col-sm-3 pl-0 pr-0" placeholder="Title"></el-input>
  </el-form-item>

  <el-form-item label="说明" prop="desc">
    <el-input v-model="form.desc" class="col-12 col-sm-5 pl-0 pr-0" placeholder="Desc"></el-input>
  </el-form-item>

  <el-form-item label="前台提示" prop="tips">
    <el-input v-model="form.tips" class="col-12 col-sm-5 pl-0 pr-0" placeholder="Tips"></el-input>
  </el-form-item>

  <el-form-item label="别名" prop="alias">
    <el-input v-model="form.alias" class="col-12 col-sm-2 pl-0 pr-0" placeholder="Alias"></el-input>
  </el-form-item>

  <el-form-item label="单位标识" prop="unit" v-if="form.type==='input'">
    <el-input v-model="form.unit" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Unit"></el-input>
  </el-form-item>

  <el-form-item label="状态" prop="status">
    <el-radio-group v-model="form.status">
      <el-radio :label="1">生效[默认]</el-radio>
      <el-radio :label="0">禁用</el-radio>
    </el-radio-group>
  </el-form-item>

  <el-form-item label="规则类型" prop="type">
    <el-radio-group v-model="form.type">
      <el-radio :label="'input'">输入框</el-radio>
      <el-radio :label="'switch'">开关</el-radio>
    </el-radio-group>
  </el-form-item>

  <el-form-item label="默认值" prop="value" v-if="form.type==='input'">
    <el-input v-model="form.value" class="col-12 col-sm-1 pl-0 pr-0" placeholder="Value"></el-input>
  </el-form-item>

  <el-form-item label="默认值" prop="value" v-if="form.type==='switch'">
    <el-switch
	  v-model="form.value"
	  active-color="#13ce66"
	  inactive-color="#6c757d"
      :active-value="1"
      :inactive-value="0">
	</el-switch>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="submitForm('form')">保存</el-button>
    <el-button @click="resetForm('form')">重置</el-button>
  </el-form-item>
</el-form>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
	{empty name="Find"}
	var url='/vip/addVipRule';
	var is_edit=false;
	var formData={
				title:'',
	      		desc:'',
	      		tips:'',
	      		alias:'',
	      		status:1,
	      		type:'input',
	      		unit:'',
	      		value:'',
			};
	var icon='';
	{else/}
	var url='/vip/editVipRule/{$Find.id}';
	var is_edit=true;
	var formData={:json_encode($Find,JSON_UNESCAPED_UNICODE)};
	{/empty}
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			labelPosition:'right',
			form:formData,
			is_edit:is_edit,
	        rules: {
		      title: [
		        { required: true, message: '请输入名称', trigger: 'blur' },
		      ],
		      alias: [
		        { required: true, message: '请输入别名', trigger: 'blur' },
		      ],
		      value: [
		        { required: true, message: '请输入或选择默认值', trigger: 'change' },
		      ],
		      status: [
		        { required: true, message: '请选择规则状态', trigger: 'change' },
		      ],
		      type: [
		        { required: true, message: '请选择规则类型', trigger: 'change' },
		      ],
	        }
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
		},
		methods:{
			submitForm:function(formName) {
				var _this=this;
				var D=_this.form;
				_this.$refs[formName].validate((valid) => {
				  if (valid) {
					const loading = XYBase.openloading();
					$.ajax({
						url:url,
						data:D,
						type:'post',
						dataType:'json',
						success:function(ret){
					        loading.close();
					        if (ret) {
								if(ret.code == 0){
									XYBase.success(ret.msg);
									if(!_this.is_edit){
										_this.resetForm(formName);
									}
								}else{
									XYBase.msg(ret.msg);
								}
					        }else{
				          		XYBase.error(XYBase.config.system_network_error)
					        }
						},
						error:function(err){
					        loading.close();
				          	XYBase.error(XYBase.config.system_network_error);
						}
					});
				  } else {
					XYBase.warning('请先把表单填写完整。');
				    return false;
				  }
				});
			},
			resetForm:function(formName) {
				this.$refs[formName].resetFields();
			},
		}
	});
$(function(){
	reloadWindowSize();
	$(window).resize(function(){
		reloadWindowSize();
	});
});
function reloadWindowSize (argument) {
	var winW=$(window).width();
	if(winW <= 576){
		vm.labelPosition='top';
	}else{
		vm.labelPosition='right';
	}
}
</script>
{/block}